<?php include "../../layout/header.php"; ?>
<?php include "../../layout/navleft.php"; ?>
<?php include "../../db/dbconnect.php"; ?>
<link rel="stylesheet" href="<?php echo($siteroot);?>css/admin.css" type="text/css" />
<script type="text/javascript" src="<?php echo($siteroot);?>js/admin/edit_item.js"></script>
<?php if(isset($role) && $role == 'A'){?>
<div id="content_medium_left">
    <div class="container_brown">
    	<font face="Verdana" color = brown size = 2.5px> <a href="admin.php" >Admin</a></font>
        <font face="Verdana" color =brown size = 2.5px> > Edit item</font><br><br>
        
        <?php
        if(isset($_POST['sort'])) $selected_sort = $_POST['sort'];
		else $selected_sort = "";
		if(isset($_POST['cat'])) $selected_cat = $_POST['cat'];
		else $selected_cat = "";
        ?>
    	<div class="admin_div">
            <form action="admin_list_item.php" method="post">
                Order Items by:
                <br><select id="sort_select" name="sort">
                    <option <?php if($selected_sort == "Name") echo "selected";?>>Name</option>
                    <option <?php if($selected_sort == "Category") echo "selected";?>>Category</option>
                    <option <?php if($selected_sort == "Price (=)") echo "selected";?>>Price (=)</option>
                    <option <?php if($selected_sort == "Price (≤)") echo "selected";?>>Price (≤)</option>
                    <option <?php if($selected_sort == "Price (≥)") echo "selected";?>>Price (≥)</option>
                    <option <?php if($selected_sort == "Level Requirement (=)") echo "selected";?>>Level Requirement (=)</option>
                    <option <?php if($selected_sort == "Level Requirement (≤)") echo "selected";?>>Level Requirement (≤)</option>
                    <option <?php if($selected_sort == "Level Requirement (≥)") echo "selected";?>>Level Requirement (≥)</option>
                </select>
                <span id="item_category_span">
                <select id="item_category" name="cat">
	                <?php
	                $query = "SELECT * FROM item_category ";
					$result = mysql_query($query);
					$dbrow = mysql_num_rows($result);
					$item_category[] = "";
					
					if($dbrow > 0){
					    while($row = mysql_fetch_array($result))
					    {
					    	$key = $row['item_cat_id'];
					    	$value = $row['cat_name'];
							$item_category[$key] = $value;
							?>
							<option value="<?php echo($key);?>"<?php if($selected_cat == $key) echo" selected";?>><?php echo($value);?></option>";
							<?php
					    }
					}
	                ?>
                </select>
                </span>
                <span id="input_query">
                	<input type="text" name="q" />
                </span><br><br>
                <input type="submit" value="Search" name="submit_button"/>
                <br><br>
            </form>

			<?php
            if(isset($_POST['submit_button'])){
            ?>
            <form name="edit_item" action="admin_list_item.php" method="post" onsubmit="return checkItem()">
                <center>
                    <table class="admin_table" border=1 cellspacing=1 cellpadding=6>
                        <tr>
                            <th width="15%">Name</th>
                            <th width="10%">Category</th>
                            <th width="10%">Price</th>
                            <th width="10%">Attack<br>Min</th>
                            <th width="10%">Attack<br>Max</th>
                            <th width="10%">Defense</th>
                            <th width="10%">Equip<br>Level</th>
                            <th width="30%">Effects</th>
                            <th width="5%">Update</th>
                        </tr>
            <?php           
                $query = "SELECT * FROM item";
                if($_POST['submit_button'] == "Search") {
                    $query .= " WHERE ";
                    if ($_POST['sort'] == "Name") {
                        $query .= "name LIKE '%".$_POST['q']."%'";
                        $query .= " ORDER BY name ASC";
                    }
                    elseif ($_POST['sort'] == "Category") {
                        $query .= "type LIKE '%".$_POST['cat']."%'";
                        $query .= " ORDER BY type ASC";
                    }
                    elseif ($_POST['sort'] == "Price (=)") {
                        $query .= "price = ".$_POST['q'];
                        $query .= " ORDER BY price ASC";
                    }
                    elseif ($_POST['sort'] == "Price (≤)") {
                        $query .= "price <= " . $_POST['q'];
                        $query .= " ORDER BY price ASC";
                        echo $query;
                    }
                    elseif ($_POST['sort'] == "Price (≥)") {
                        $query .= "price >= " . $_POST['q'];
                        $query .= " ORDER BY price ASC";
                    }
                    elseif ($_POST['sort'] == "Level Requirement (=)") {
                        $query .= "level = ".$_POST['q'];
                        $query .= " ORDER BY level ASC";
                    }
                    elseif ($_POST['sort'] == "Level Requirement (≤)") {
                        $query .= "level <= ".$_POST['q'];
                        $query .= " ORDER BY level ASC";
                    }
                    elseif ($_POST['sort'] == "Level Requirement (≥)") {
                        $query .= "level >= ".$_POST['q'];
                        $query .= " ORDER BY level ASC";
                    }
                } elseif ($_POST['submit_button'] == "Update") {
                    $updateArray = $_POST['updateItem'];

                    foreach ($updateArray as $updateItem) {
                        $updateStatement = "UPDATE item SET ";
                        $newName = trim($_POST['iName'.$updateItem]);
                        $newPrice = trim($_POST['iPrice'.$updateItem]);
                        $newType = trim($_POST['iType'.$updateItem]);
                        $newAtkMin = trim($_POST['iAtkMin'.$updateItem]);
                        $newAtkMax = trim($_POST['iAtkMax'.$updateItem]);
                        $newDefense = trim($_POST['iDefense'.$updateItem]);
                        $newLevel = trim($_POST['iLevel'.$updateItem]);
                        $newEffects = trim($_POST['iEffects'.$updateItem]);
                        $validUpdate = 0;
                        if (!empty($newName)) {
                            $validUpdate = 1;
                            $updateStatement .= "name=\"".$newName."\"";
                        }
                        if (!empty($newType)) {
                            if ($validUpdate > 0) {
                                $updateStatement .= ", ";
                            }
                            $validUpdate = 1;
                            $updateStatement .= "type=".$newType."";
                        }
                        if (!empty($newPrice)) {
                            if ($validUpdate > 0) {
                                $updateStatement .= ", ";
                            }
                            $validUpdate = 1;
                            $updateStatement .= "price_buy = ".$newPrice." ";
                        }
                        if (!empty($newAtkMin)) {
                            if ($validUpdate > 0) {
                                $updateStatement .= ", ";
                            }
                            $validUpdate = 1;
                            $updateStatement .= "atk_min = ".$newAtkMin." ";
                        }
                        if (!empty($newAtkMax)) {
                            if ($validUpdate > 0) {
                                $updateStatement .= ", ";
                            }
                            $validUpdate = 1;
                            $updateStatement .= "atk_max = ".$newAtkMax." ";
                        }
                        if (!empty($newDefense)) {
                            if ($validUpdate > 0) {
                                $updateStatement .= ", ";
                            }
                            $validUpdate = 1;
                            $updateStatement .= "defense = ".$newDefense." ";
                        }
                        if (!empty($newLevel)) {
                            if ($validUpdate > 0) {
                                $updateStatement .= ", ";
                            }
                            $validUpdate = 1;
                            $updateStatement .= "equip_level=".$newLevel." ";
                        }
                        if (!empty($newEffects)) {
                            if ($validUpdate > 0) {
                                $updateStatement .= ", ";
                            }
                            $validUpdate = 1;
                            $updateStatement .= "effects=\"".$newEffects."\"";
                        }
                        $updateStatement .= "WHERE item_id=".$updateItem;
                        if ($validUpdate > 0) {
                            mysql_query($updateStatement);
                        }
                    }
                }
                $rs = mysql_query($query);
                if($rs) $cnt = mysql_num_rows($rs);
                else $cnt = 0;
                echo "<p align=\"left\">Search Result: {$cnt}</p>";
				if($cnt > 0){
                while ($row = mysql_fetch_array($rs)) {
                ?>
                    <tr>
                    	<td>
                    		<font style='visibility:hidden; position:absolute; left:0'><?php echo($row['name']);?></font>
                    		<input type="text" style="Width:100px;" value="<?php echo($row['name']);?>" name="iName<?php echo($row['item_id']);?>">
                    	</td>
                    	<td>
                    		<font style='visibility:hidden; position:absolute; left:0'><?php echo($row['type']);?></font>
                    		<select style="Width:60px;" name="iType<?php echo($row['item_id']);?>">
                    		<?php
		                    foreach ($item_category as $key => $val){
		                    	if($item_category[$key] != ""){
									echo "<option value=\"{$key}\"";
									if($row['type'] == $key) echo " selected";
									echo ">{$val}</option>";
		                    	}
		                    }
		                    ?>
							</select>
						</td>
                    	<td>
                    		<font style='visibility:hidden; position:absolute; left:0'><?php echo($row['price_buy']);?></font>
                    		<input type="text" style="Width:42px;" value="<?php echo($row['price_buy']);?>" name="iPrice<?php echo($row['item_id']);?>">
                    	</td>
                    	<td>
                    		<font style='visibility:hidden; position:absolute; left:0'><?php echo($row['atk_min']);?></font>
                    		<input type="text" style="Width:30px;" value="<?php echo($row['atk_min']);?>" name="iAtkMin<?php echo($row['item_id']);?>">
                    	</td>
                    	<td>
                    		<font style='visibility:hidden; position:absolute; left:0'><?php echo($row['atk_max']);?></font>
                    		<input type="text" style="Width:30px;" value="<?php echo($row['atk_max']);?>" name="iAtkMax<?php echo($row['item_id']);?>">
                    	</td>
                    	<td>
                    		<font style='visibility:hidden; position:absolute; left:0'><?php echo($row['defense']);?></font>
                    		<input type="text" style="Width:30px;" value="<?php echo($row['defense']);?>" name="iDefense<?php echo($row['item_id']);?>">
                    	</td>
                    	<td>
                    		<font style='visibility:hidden; position:absolute; left:0'><?php echo($row['equip_level']);?></font>
                    		<input type="text" style="Width:30px;" value="<?php echo($row['equip_level']);?>" name="iLevel<?php echo($row['item_id']);?>">
                    	</td>
                    	<td>
                    		<font style='visibility:hidden; position:absolute; left:0'><?php echo($row['effects']);?></font>
                    		<input type="text" style="Width:150px;" value="<?php echo($row['effects']);?>" name="iEffects<?php echo($row['item_id']);?>">
                    	</td>
                    	<td>
							<input type="checkbox" value="<?php echo($row['item_id']);?>" name="updateItem[]">
						</td>
                    </tr>
                <?php
                }
				}
                ?>
            	</table></center><br>
            	<?php
            	if($cnt > 0){
            	?>
                <table style="width:100%">
                    <tr>
                        <td style="text-align: right">
                            <input type="submit" name ="submit_button" value="Update"/>
                        </td>
                    </tr>
                </table>
                <?php
            	}
            	?>
            </form>
            <?php
            }
            ?>
                    
        </div>
	</div>
</div>
<?php }else{?>
<div id="content_small">
    <div class="container_brown">
    	<font face="Verdana" color = brown size = 2.5px> <a href="admin.php" >Admin</a></font>
        <font face="Verdana" color =brown size = 2.5px> > Edit item</font><br><br>
		<br><br>
		<p class="admin_head">This page is for administrator only.</p>
		<br><br>
	</div>
</div>
<?php include "../../layout/navright.php"; }?>
<?php include "../../layout/footer.php"; ?>